﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.API.Applcation.Command.OutBoundCommand;
using Warehouse.API.Applcation.CommandHandlers.OutShipCommandHandlers;
using Warehouse.Domain.DTO;

namespace Warehouse.API.Controllers
{
    /// <summary>
    /// 出库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class OutBoundController : ControllerBase
    {
        private readonly IMediator _media;
        


        public OutBoundController(IMediator media)
        {
             _media = media;
        }

        /// <summary>
        /// 新增出库单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddOutShip(AddOutShipDTO command)
        {
             ApiResult<int> result = new ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.OPlist.FirstOrDefault().office_id = sid;
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.user_id = Convert.ToInt32(uid);
                command.AddName = name;
                command.OPlist.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Data = await _media.Send(new CreateOutBoundCommand {  OutBound = command });
            
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }





        /// <summary>
        /// 删除出库单
        /// </summary>
        /// <param name="commmand"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelOutShip(DelOutShipCommand commmand)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {  

                result.Data = await _media.Send(commmand);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 更新出库单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UptOutShip(UptOutShipDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id= sid;
                command.OutProducts.FirstOrDefault().office_id = sid;
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                
                command.user_id = Convert.ToInt32(uid);
                command.OutProducts.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Data = await _media.Send(new UptOutShipCommand { UptDto = command });
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



    }
}
